package com.glory.dgcpcpda.activity.technicalservice;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.mfg.EquipmentSchedulingActivity;
import com.glory.dgcpcpda.adapter.SchedulingAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.SchedulingEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class QueryEquipmentSchedulingActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.etDispatchUser)
    FreeUI_ClearEditText etDispatchUser;
    @BindView(R.id.etLocation)
    FreeUI_ClearEditText etLocation;
    @BindView(R.id.rvSchedulingList)
    RecyclerView rvSchedulingList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private SchedulingAdapter mSchedulingAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_query_equipment_scheduling;
    }

    @Override
    protected void initView() {
        tvTitle.setText("设备调度查询");
        tvBaseSubmit.setText("查询");
        etDispatchUser.setText(Constant.CURRENT_USER);
        setMainLayout(llMain);
        initRecyclerView(rvSchedulingList, this);
        mSchedulingAdapter = new SchedulingAdapter(null);
        rvSchedulingList.setAdapter(mSchedulingAdapter);
        mSchedulingAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {

        if (getText(etDispatchUser).equals("")){
            showToast("请输入工号",false);
            return;
        }

        queryDispatch();
    }

    private void queryDispatch() {
        FreeUI_EntityCallBack<BaseResponseBean<SchedulingEntity>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<SchedulingEntity>>
                (new TypeToken<BaseResponseBean<SchedulingEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<SchedulingEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    mSchedulingAdapter.setNewData(response.body().getResponse().getBody().getEquipmentlist());
                }
            }
            @Override
            public void onError(Response<BaseResponseBean<SchedulingEntity>> response) {
                super.onError(response);
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("DISPATCHUSER", getText(etDispatchUser));
        map.addProperty("EQUIPMENTID", getText(etEqpID));
        map.addProperty("LOCATION", getText(etLocation));
        NetUtils.requestPostNet("CPC.QUERYDISPATCH", "TRANSFERMES",this, map, callback);

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("DATA", mSchedulingAdapter.getItem(position));
        FreeApi_Utils.skipWithExtraIntent("", bundle, QueryEquipmentSchedulingActivity.this, EquipmentSchedulingActivity.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mSchedulingAdapter.getData().size() != 0){
            queryDispatch();
        }
    }
}
